package com.lvyq.qrobot.job;

import com.lvyq.qrobot.common.UrlConstant;
import com.lvyq.qrobot.entity.Log;
import com.lvyq.qrobot.entity.UserEntity;
import com.lvyq.qrobot.entity.UserGovAffair;
import com.lvyq.qrobot.mapper.LogMapper;
import com.lvyq.qrobot.mapper.UserGovAffairMapper;
import com.lvyq.qrobot.mapper.UserMapper;
import com.lvyq.qrobot.mapper.UserMsgMapper;
import com.lvyq.qrobot.model.send.SendParam;
import com.lvyq.qrobot.util.ChuanYueUtil;
import com.lvyq.qrobot.util.HttpClientUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author lvyq
 * @version 1.0
 * Create by 2024-05-21 15:57
 */

@Component
public class MyJob {

    public static final String tnk="Ybk_aD00p9a9LwqbLBanGD6Hetb3na5PiWloaHMXu3Y";
    public static final String i="22177190";
    public static UserMapper userMapper;

    public static LogMapper logMapper;

    public static UserGovAffairMapper userGovAffairMapper;

    public static UserMsgMapper userMsgMapper;

    @Autowired
    public  void setUserMsgMapper(UserMsgMapper userMsgMapper) {
        MyJob.userMsgMapper = userMsgMapper;
    }

    @Autowired
    public  void setUserMapper(UserMapper userMapper) {
        MyJob.userMapper = userMapper;
    }

    @Autowired
    public  void setUserGovAffairMapper(UserGovAffairMapper userGovAffairMapper) {
        MyJob.userGovAffairMapper = userGovAffairMapper;
    }

    @Autowired
    public  void setLogMapper(LogMapper logMapper) {
        MyJob.logMapper = logMapper;
    }

    /**
     * @Description: 打卡任务重置- 每日凌晨重置
     * @params:  * @param
     * @return:   秒 分 时 日 月 周
     * @Author: lvyq
     * @Date: 2023/3/19 22:20
     */
    @Scheduled(cron = "0 0 9 * * ?")
    public void signInJob(){
        //重置打卡状态、每日政务、每日礼包领取次数
        userMapper.reSetSingIn();
    }


    @Scheduled(cron = "0 0 9 * * ?")
    public void sayHello(){
        List<Integer> groups =  new ArrayList<>();
        groups.add(486890378);
        groups.add(495673338);
        groups.add(491922566);
       // List<String> contents = sayHelloRepository.findAllByType(0);
        groups.forEach(g->{
            //Integer index = CommonUtil.randomNum(contents.size());
            SendParam param = new SendParam();
            param.setMessage("每一个清晨都是新的开始，向群里的朋友们送上温馨的早安问候：早上好，愿你们的一天充满阳光！");
            param.setGroup_id(g);
            HttpClientUtil.doPostJson(UrlConstant.send_group_msg,param);
        });
    }

    @Scheduled(cron = "0 30 17 * * ?")
    public void sayGoodBye(){
        List<Integer> groups =  new ArrayList<>();
        groups.add(486890378);
        groups.add(495673338);
        groups.add(491922566);
       /* List<String> contents = sayHelloRepository.findAllByType(1);
        groups.forEach(g->{
            Integer index = CommonUtil.randomNum(contents.size());
            SendParam param = new SendParam();
            param.setMessage(contents.get(index));
            param.setGroup_id(g);
            HttpClientUtil.doPostJson(HttpUrlConstant.send_group_msg,param);
        });*/
    }

    /**
     * @description 每30秒执行一次,政务
     * @author lvyq
     * @throws
     * @time 2024-06-04 15:57
     */
   // @Scheduled(fixedRate = 1000*30)
    public void userGovAffair(){
        System.out.println("检测政务任务完成状态");
        List<UserGovAffair> userGovAffairs = userGovAffairMapper.listAll();

        userGovAffairs.forEach(userGovAffair -> {
            if (new Date().getTime()>userGovAffair.end){
                SendParam param = new SendParam();
                param.setAuto_escape(false);
                param.setGroup_id(userGovAffair.groupId);
                //完成任务
                UserEntity user = userMapper.selectByUserId(userGovAffair.userId, userGovAffair.groupId);
                userGovAffairMapper.deleteById(userGovAffair.id);
                user.setAffairingNum(user.affairingNum+1);
                user.setJingyan(user.jingyan+userGovAffair.jingyan);
                user.setTili(user.tili-userGovAffair.tili);
                userMapper.updateById(user);
                String content = "[CQ:at,qq={1}]\n恭喜你完成了["+userGovAffair.name+"]~\n体力-{2}\n经验：\n{3}";
                content=content.replace("{1}",userGovAffair.userId.toString())
                                .replace("{2}",userGovAffair.tili.toString())
                                        .replace("{3}",userGovAffair.jingyan.toString());
               param.setMessage(content);
                HttpClientUtil.doPostJson(UrlConstant.send_group_msg,param);
            }
        });
    }

    /**
     * @description 帮派动态
     * @author lvyq
     * @throws
     * @time 2024-06-24 10:39
     */
    @Scheduled(fixedRate = 1000*60)
    public void userGovAffair2(){
        SendParam param = new SendParam();
        param.setGroup_id(957620729);
        bpdt(param);
        gldt(param);

    }

    //福利动态
    private void gldt(SendParam param) {
        String URL="http://game.qqhome.cn/mergecyhc5/family/reward_w2.jsp?i="+i+"&tkn="+tnk;
        String s1 = Jsoup.parse(ChuanYueUtil.sendGet(URL)).body().getElementsByClass("module-content").toString();
        Document parse = Jsoup.parse(s1);
        Element a = parse.getElementsByTag("a").get(3);
        String name = a.text();
        String text = parse.text();
        //剩余
        String gz = text.split("领取工资")[0].split("\\:")[1];
        String content = text.split("2\\.")[0].split("1\\.")[1];
        SimpleDateFormat format = new SimpleDateFormat("MM-dd");
        String time = format.format(new Date());
        Log log = logMapper.selBytext(content);
        if (log==null){
            log = new Log();
            log.setText(content);
            log.setType(2);
            logMapper.insert(log);
            content =content.replace(time,"").replace(name,"["+name+"]");
            content=content.replace("[]","");
            param.setMessage("[CQ:face,big=false,id=295]【福利动态】  工资剩:"+gz+"\n"+content);
            HttpClientUtil.doPostJson(UrlConstant.send_group_msg,param);
            System.out.println(content);
        }
    }

    //帮派动态
    private void bpdt(SendParam param) {
        String URL="http://game.qqhome.cn/mergecyhc5/family/family_trend_w2.jsp?i="+i+"&tkn="+tnk;
        String s1 = Jsoup.parse( ChuanYueUtil.sendGet(URL)).body().getElementsByClass("module-content").toString();
        Document parse = Jsoup.parse(s1);
        Element a = parse.getElementsByTag("a").get(0);
        String name = a.text();
        String text = parse.text();
        String content = text.split("2\\.")[0].split("1\\.")[1];
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String time = format.format(new Date());
        Log log = logMapper.selBytext(content);
        if (log==null){
            log = new Log();
            log.setText(content);
            log.setType(1);
            logMapper.insert(log);
            content =content.replace(time,"").replace(name,"["+name+"]");
            param.setMessage("[CQ:face,big=true,id=343]【帮派动态】[CQ:face,big=true,id=343]\n"+content);
            HttpClientUtil.doPostJson(UrlConstant.send_group_msg,param);
            System.out.println(content);
        }
    }


    @Scheduled(cron = "0 1 0 * * ?")
    public void qiandao(){
        SendParam param = new SendParam();
        param.setGroup_id(957620729);
        int i=22177190;
        String tkn="Ybk_aD00p9a9LwqbLBanGD6Hetb3na5PiWloaHMXu3Y";
        StringBuffer buffer = new StringBuffer();
        buffer.append("[CQ:at,qq=1540194374]\n");
        String sing = ChuanYueUtil.sing(i, tkn);
        String choujiang = ChuanYueUtil.choujiang(i, tkn);
        String shangchao = ChuanYueUtil.shangchao(i, tkn);
        String jingji = ChuanYueUtil.jingji(i, tkn);
        String yaoqian = ChuanYueUtil.yaoqian(i, tkn);
        ChuanYueUtil.lilian(i,tkn);
        buffer.append(sing)
                .append("\n"+shangchao)
                .append("\n"+choujiang)
                .append("\n"+jingji)
                .append("\n"+yaoqian);
        param.setMessage(buffer.toString());
        HttpClientUtil.doPostJson(UrlConstant.send_group_msg,param);
    }


    @Scheduled(cron = "0 0 13 * * ?")
    public void laoyu(){
        SendParam param = new SendParam();
        param.setGroup_id(957620729);
        int i=22177190;
        String tkn="Ybk_aD00p9a9LwqbLBanGD6Hetb3na5PiWloaHMXu3Y";
        param.setMessage("天牢劫狱活动开启");
        HttpClientUtil.doPostJson(UrlConstant.send_group_msg,param);
        ChuanYueUtil.lao(i,tkn);
    }

    @Scheduled(cron = "30 30 11 * * ?")
    public void shenmiren(){
        SendParam param = new SendParam();
        param.setGroup_id(957620729);
        int i=22177190;
        String tkn="Ybk_aD00p9a9LwqbLBanGD6Hetb3na5PiWloaHMXu3Y";
        param.setMessage("神秘人活动");
        HttpClientUtil.doPostJson(UrlConstant.send_group_msg,param);
        ChuanYueUtil.shenmiren(i,tkn);
    }
    @Scheduled(cron = "30 0 12 * * ?")
    public void shenmirenlj(){
        SendParam param = new SendParam();
        param.setGroup_id(957620729);
        int i=22177190;
        String tkn="Ybk_aD00p9a9LwqbLBanGD6Hetb3na5PiWloaHMXu3Y";
        param.setMessage("神秘人活动奖励可以领取了");
        HttpClientUtil.doPostJson(UrlConstant.send_group_msg,param);
        ChuanYueUtil.shenmirenlq(i,tkn);
    }

    @Scheduled(cron = "30 0 21 * * ?")
    public void shenmiren2(){
        SendParam param = new SendParam();
        param.setGroup_id(957620729);
        int i=22177190;
        String tkn="Ybk_aD00p9a9LwqbLBanGD6Hetb3na5PiWloaHMXu3Y";
        param.setMessage("神秘人活动");
        HttpClientUtil.doPostJson(UrlConstant.send_group_msg,param);
        ChuanYueUtil.shenmiren(i,tkn);
    }
    @Scheduled(cron = "30 30 21 * * ?")
    public void shenmirenlq2(){
        SendParam param = new SendParam();
        param.setGroup_id(957620729);
        int i=22177190;
        String tkn="Ybk_aD00p9a9LwqbLBanGD6Hetb3na5PiWloaHMXu3Y";
        param.setMessage("神秘人活动奖励可以领取了");
        HttpClientUtil.doPostJson(UrlConstant.send_group_msg,param);
        ChuanYueUtil.shenmirenlq(i,tkn);
    }

    @Scheduled(cron = "0 0 21 * * ?")
    public void count(){
        SendParam param = new SendParam();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        List<Map<String, Object>> maps = userMsgMapper.totalMsgCount(957620729, format.format(new Date()));
        StringBuffer buffer = new StringBuffer();
        buffer.append("\uD83D\uDCE3今日发言统计\uD83D\uDCE3\n");
        AtomicInteger index = new AtomicInteger();
        maps.forEach(map->{
            index.getAndIncrement();
            buffer.append(index+"."+map.get("name")+"("+ map.get("count")+")");
            map.get("name");
        });
        param.setGroup_id(957620729);
        param.setMessage(buffer.toString());
        HttpClientUtil.doPostJson(UrlConstant.send_group_msg,param);
    }


}

